package com.winning.sx.microframework.model.genericapi;

import com.winning.sx.microframework.common.constant.GenericApiConstant;
import com.winning.sx.microframework.model.exception.CustomException;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Map;

@Data public class GenericApiInterfaceLog {

    private Map<String,Object> paramMap;

    private int id;
    private int interfaceId;

    private String logLevel = "Error";
    private String interfaceName;
    private String interfacePattern;
    private String visitAddress;
    private String sendContent;
    private String returnContent;
    private String keyWord1;
    private String keyWord2;
    private String keyWord5;
    private String keyWord4;
    private String keyWord6;
    private String keyWord7;
    private String keyWord8;
    private String keyWord9;
    private String keyWord10;
    private String exceptionMessage;
    private Timestamp createTime = new Timestamp(System.currentTimeMillis());
    private String updateTime;
    private String validFlag;
    private Long start = System.currentTimeMillis();


    public GenericApiInterfaceLog(String data, String requestIP){
        super();
        this.sendContent = data;
        setVisitAddress(requestIP);
    }

    public void recordInterfaceInfo(GenericApiInterfaceInfo apiInterfaceInfo) throws CustomException {
        try{
            this.interfaceId = apiInterfaceInfo.getId();
            this.interfaceName = apiInterfaceInfo.getName();
            this.interfacePattern = apiInterfaceInfo.getCurrentPattern();
        }catch (NullPointerException e){
            throw new CustomException(GenericApiConstant.ERROR_CODE_UNKNOW_INTERFACE,"接口信息不全！");
        }
    }

    public void caculateTime(){
        this.updateTime = String.valueOf(System.currentTimeMillis() - start);
    }

}
